# #!/usr/bin/env python
# # -*- encoding: utf-8 -*-
# '''
# @File    :   APP_TransportLayer_test.py
# @Time    :   2025/03/20 13:31:28
# @Author  :   YangLiang 
# @Version :   1.0
# @Contact :   yangl@genvict.com
# @License :   (C)Copyright 2022-2025
# @Desc    :   UDS NM 测试用例
# '''

# import pytest
# import allure
# import time
# from pytest_assume.plugin import assume
# import Set_up
# from commDriver.TS_CAN import *
# import threading

# class Test_UDS_NM:

#     @allure.severity('critical')
#     @allure.title('网络管理状态转换测试')
#     @allure.description('''验证DUT网络管理状态转换流程：
#     1. DUT上电进入BSM状态
#     2. 通过NM报文唤醒DUT到RMS状态
#     3. 停止NM报文触发NOS状态
#     4. 验证本地唤醒事件后的状态转换''')
#     def test_nm_state_transition2(self, action, Driver):
#         iStep = 1
#         iStep = Driver['CAN'].setSignalEanble(0x90, False, 0.5, iStep)
#         # 步骤1：确保DUT断电状态
#         logging.info(f"步骤{iStep}: 确保DUT断电状态")
#         logging.info('wait 2s')
#         Driver['power'].turn_off_power()
#         time.sleep(2)  # 确保完全断电
#         iStep += 1
        
#         # 步骤2：上电并等待进入BSM状态
#         logging.info(f"步骤{iStep}: 上电并等待进入BSM状态")
#         Driver['power'].turn_on_power()
#         logging.info('wait 15s')
#         time.sleep(15)  # 等待进入睡眠模式
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RMS_state['error'] == "Invalid NM frame data", "DUT未进入BSM状态"
#         iStep += 1

#         # 步骤3：发送NM报文唤醒DUT
#         Driver['CAN'].send_frame(0x57F, [0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], type = 'CAN', print=True)
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert  RMS_state['RMS_state'] == 1, "DUT未进入RMS状态"
#         iStep += 1

#         # 步骤4：停止NM报文触发NOS状态
#         time.sleep(5)
#         rms_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RMS_state['error'] == "Invalid NM frame data", "DUT未进入BSM状态"
#         iStep += 1

        
#         # 步骤3：发送NM报文唤醒DUT
#         Driver['CAN'].send_frame(0x57F, [0x7F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], type = 'CAN', print=True)
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert  RMS_state['RMS_state'] == 1, "DUT未进入RMS状态"


#     def test_nm_state_transition(self, action, Driver):
#         iStep = 1
#         iStep = Driver['CAN'].setSignalEanble(0x90, False,iStep)
#         # 步骤1：确保DUT断电状态
#         logging.info(f"步骤{iStep}: 关闭电源确保DUT断电状态")
#         logging.info('wait 2s')
#         Driver['power'].turn_off_power()
#         time.sleep(2)  # 确保完全断电
#         iStep += 1
        
#         # 步骤2：上电并等待进入BSM状态
#         logging.info(f"步骤{iStep}: 上电并等待进入BSM状态")
#         Driver['power'].turn_on_power()
#         logging.info('wait 5s')
#         time.sleep(5)  # 等待进入睡眠模式
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RMS_state['error'] == "Invalid NM frame data", "DUT未进入BSM状态"
#         iStep += 1

#         # 步骤3：发送NM报文唤醒DUT
#         new_signal = {
#             'id': 0x57F,  # 新信号ID
#             'cycle_ms': 500,  # 发送周期200ms
#             'is_canfd': False,  # 使用CANFD
#             'data': [0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]  # 信号数据
#         }
#         Driver['CAN']._add_single_signal(new_signal)
#         time.sleep(1)
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RMS_state['RMS_state'] == 1, logging.error("DUT未进入RMS状态")
#         iStep += 1

#         # 步骤4：等待CanNmRepeatMessageTime后检测RSS状态
#         logging.info(f"步骤{iStep}: 等待CanNmRepeatMessageTime后检测RSS状态")
#         Driver['CAN'].clear_buffer()
#         logging.info('wait 1s')
#         time.sleep(1)  # 假设CanNmRepeatMessageTime为5秒
#         RSS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RSS_state['error'] == 'Invalid NM frame data', logging.error("DUT未进入RSS状态")
#         iStep += 1

#         # 步骤5：使用新函数启动线程发送NM报文
#         Driver['CAN'].clear_buffer()
#         time.sleep(1)
#         Driver['CAN'].add_condition_monitor(0x57F, [0x7F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
#         rxTimestamp = Driver['CAN'].add_condition_monitor(0x563, [0x63, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xBF])
#         time.sleep(1)  # 等待CanNmRepeatMessageTime后检测RSS状态
#         logging.info(f"步骤{iStep}: 发送NM_CAN_Test_1并检测RMS状态")
#         Driver['CAN'].update_signal_data(ID = 0X57F, index = [1], valueBytes = [0x01])
#         RMS_state = Driver['CAN'].check_nm_state(Driver)
#         with assume: assert RMS_state['RMS_state'] == 1, logging.error("DUT未进入RMS状态")
#         iStep += 1

#         # 步骤6：测量响应时间T
#         time.sleep(2)
#         logging.info(f"步骤{iStep}: 测量响应时间")
#         txTimestamp = Driver['CAN'].get_first_match_timestamp(0x57F)
#         rxTimestamp = Driver['CAN'].get_first_match_timestamp(0x563)
#         response_time = round((rxTimestamp - txTimestamp) * 1000,2)
#         logging.info(f'响应时间是{response_time}')  # 转换为毫秒
#         with assume: assert response_time <= 30, logging.error(f"响应时间T={response_time}ms超过30ms限制")

